// Decompiled by Jad v1.5.8g. Copyright 2001 Pavel Kouznetsov.
// Jad home page: http://www.kpdus.com/jad.html
// Decompiler options: fullnames 
// Source File Name:   AgentWorker.java

package app;

import java.io.ByteArrayOutputStream;
import java.util.Vector;


// Referenced classes of package app:
//            ParagrahpElement, Hyperlink, ImageElement, ArabicElement, 
//            ChoiceElement, Utils

public class AgentWorker
    implements java.lang.Runnable
{
    public static final int MAXIMUM_SIZE = 10240;
    private static final int STATE_PAGE = 0;
    private static final int STATE_HYP = 1;
    private static final int STATE_HYP_TITLE = 2;
    private static final int STATE_HEADER = 3;
    private static final int STATE_IMG = 4;
    private static final int STATE_BOLD = 5;
    private static final int STATE_BOLD_JUDUL = 6;
    private static final int STATE_ARABIC = 7;
    private static final int STATE_CHOICE = 8;
    private static final int STATE_BAB = 9;
    private boolean running;
    private java.lang.Object lock;
    private java.util.Vector queue;

    public AgentWorker()
    {
        running = true;
        lock = this;
        queue = new Vector();
        (new Thread(this)).start();
    }

    public void stopSignal()
    {
        running = false;
        synchronized(lock)
        {
            lock.notify();
        }
    }

    public void addQueue(java.lang.String query, java.lang.Object listener, java.lang.Integer top)
    {
        java.lang.Object o[] = new java.lang.Object[3];
        o[0] = query;
        o[1] = listener;
        o[2] = top;
        synchronized(queue)
        {
            queue.addElement(((java.lang.Object) (o)));
        }
        synchronized(lock)
        {
            lock.notify();
        }
    }

    public void stopAll()
    {
        synchronized(queue)
        {
            queue.removeAllElements();
        }
    }

    public void run()
    {
        do
        {
            if(!running)
                break;
            java.lang.Object obj[] = null;
            synchronized(queue)
            {
                if(queue.size() > 0)
                {
                    obj = (java.lang.Object[])(java.lang.Object[])queue.elementAt(0);
                    queue.removeElementAt(0);
                }
            }
            if(obj == null)
            {
                synchronized(lock)
                {
                    try
                    {
                        wait();
                    }
                    catch(java.lang.InterruptedException e) { }
                }
                if(!running)
                    break;
            } else
            {
                java.io.InputStream inputstream = null;
                inputstream = getClass().getResourceAsStream(obj[0].toString());
                try
                {
                    parsePage(inputstream, (ui.Page)obj[1], false, (java.lang.Integer)obj[2]);
                }
                catch(java.io.IOException ex)
                {
                    ex.printStackTrace();
                }
            }
        } while(true);
    }

    private void parsePage(java.io.InputStream inputStream, ui.Page page, boolean fresh, java.lang.Integer top)
        throws java.io.IOException
    {
        byte buf1[];
        java.io.ByteArrayOutputStream buffer1;
        int width;
        int state;
        java.util.Vector varg;
        int y;
        int total;
        int start;
        buf1 = new byte[64];
        buffer1 = new ByteArrayOutputStream();
        width = page.getWidth();
        state = 0;
        varg = new Vector();
        y = 0;
        total = 0;
        start = top != null ? top.intValue() + 1 : 1;
        int r;
        int i;
        //if((r = inputStream.read(buf1)) <= 0)
            //break MISSING_BLOCK_LABEL_1535;
            //return;
        //if(!fresh);
        //total += r;
        i = 0;
        //byte ch;
        int ch = 0;
        
        while((ch = inputStream.read())!=-1) {
                if(ch == 13 || ch == 10)
                {
                    //kalo enter, mulai parsing
                    if(state==STATE_ARABIC) {
                        if(buffer1.size()>0) {
                            app.ArabicElement he = new ArabicElement(buffer1.toByteArray(), 0, y, width);
                            y += ((ui.Element) (he)).height;
                            page.addElement(he, start);
                        }
                        state = STATE_PAGE;
                        buffer1.reset();
                    } else if(state==STATE_BOLD_JUDUL) {
                        if(buffer1.size() > 1)
                        {
                            java.lang.String title = new String(buffer1.toByteArray());
                            ui.Element t = new ParagrahpElement(title, 0, y, width, 2, 0);
                            y += t.height;
                            page.addElement(t, start);
                        }
                        state = STATE_PAGE;
                        buffer1.reset();
                    } else if(state==STATE_HYP) {
                        if(buffer1.size() > 1)
                        {
                            String bahan = buffer1.toString();
                            int at = bahan.indexOf("]");
                            String url = bahan.substring(0, at);
                            
                            String title = bahan.substring(bahan.indexOf("]")+1,bahan.length());
                            app.Hyperlink h = new Hyperlink(title, url, 0, y, width, true);
                            y += h.getHeight();
                            page.addElement(h, start);
                        }
                        state = STATE_PAGE;
                        buffer1.reset();
                    } else if(state==STATE_IMG) {
                        if(buffer1.size() > 1)
                        {
                            String bahan = buffer1.toString();
                            int at = bahan.indexOf(">");
                            String urlimg = bahan.substring(0, at);
                            
                            //app.ImageElement e = new ImageElement(urlimg, 0, y, w, h);
                            app.ImageElement e = new ImageElement(urlimg, 0, y);
                            y += e.getHeight();
                            page.addElement(e, start);
                        }
                        state = STATE_PAGE;
                        buffer1.reset();
                    } else {
                        if(buffer1.size() > 1)
                        {
                            app.ParagrahpElement e = new ParagrahpElement(buffer1.toString(), 0, y, width, 0, 0);
                            y += ((ui.Element) (e)).height;
                            page.addElement(e, start);
                        } else {
                            app.ParagrahpElement e = new ParagrahpElement("", 0, y, width, 0, 0);
                            y += ((ui.Element) (e)).height;
                            page.addElement(e, start);
                        }
                        state = STATE_PAGE;
                        buffer1.reset();
                    }
                } else if (ch==36) {
                    state = STATE_ARABIC;
                    buffer1.reset();
                } else if (ch==42) {
                    state = STATE_BOLD_JUDUL;
                    buffer1.reset();
                } else if (ch==60) {
                    state = STATE_IMG;
                    buffer1.reset();
                } else if (ch==91) {
                    state = STATE_HYP;
                    buffer1.reset();
                } else if(ch != 10 && ch != 13) {
                    buffer1.write(ch);
                }
        }
    }
}
